//
//  YBQuestionnaireListViewController.m
//  HomeDoorWay
//
//  Created by apple on 17/11/16.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "YBQuestionnaireListViewController.h"
#import "YBQuestionnaireListTableViewCell.h"
#import "ZJWebViewController.h"
#import "YBRequestManager.h"
#import "UITableView+iOS11.h"
#define QuestionnaireCell @"YBQuestionnaireListTableViewCell"

@interface YBQuestionnaireListViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , strong) UITableView * mainTableView;

@property (nonatomic , strong) NSMutableArray<YBQuestionnaireModel *> * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation YBQuestionnaireListViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.mainTableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titlelabel.text = @"问卷调查";
    [self ybInitUserInterface];
    
}

-(void)ybInitUserInterface{
    UIImageView * headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenWith] * 0.425)];
    headImageView.image = [UIImage imageNamed:@"wjdc_banner"];
    [self.view addSubview:headImageView];
    
    __weak typeof(self) weakSelf = self;
    
    [self.refurbish headerToScollview:self.mainTableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.mainTableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self layoutUI];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_questionnaireListByPage:[NSString stringWithFormat:@"%ld",self.page] successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [weakSelf.dataArray removeAllObjects];
            NSArray *array = [object objectForKey:@"PSQList"];
            NSArray * tempArray = [NSArray yy_modelArrayWithClass:[YBQuestionnaireModel class] json:array];
            if (tempArray.count > 0) {
                for (YBQuestionnaireModel * model in tempArray) {
                    [model calculateCellHeight];
                    [weakSelf.dataArray addObject:model];
                }
            }
            [weakSelf.mainTableView reloadData];
        }
        
        [weakSelf.mainTableView.mj_header endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.mainTableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:weakSelf.view];
    }];
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_questionnaireListByPage:[NSString stringWithFormat:@"%ld",self.page] successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"PSQList"];
            NSArray * tempArray = [NSArray yy_modelArrayWithClass:[YBQuestionnaireModel class] json:array];
            if (tempArray.count > 0) {
                for (YBQuestionnaireModel * model in tempArray) {
                    [model calculateCellHeight];
                    [weakSelf.dataArray addObject:model];
                }
            }
            [weakSelf.mainTableView reloadData];
        }
        [weakSelf.mainTableView.mj_footer endRefreshing];
        
    } failBlock:^(id object) {
        [weakSelf.mainTableView.mj_footer endRefreshing];
    }];
    
}


- (void)layoutUI{
    [self.view addSubview:self.mainTableView];
    TenGrayShadowView *header = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10 * [UIScreen zj_screenHScale])];
    self.mainTableView.tableHeaderView = header;
}


#pragma mark - 协议代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArray[indexPath.row].cellHeight;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBQuestionnaireListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:QuestionnaireCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJWebViewController *vc = [ZJWebViewController new];
    vc.titleString = @"问卷详情";
    YBQuestionnaireModel *model = self.dataArray[indexPath.row];
    if ([model.ANS_STATE isEqualToString:@"1"]) {//已答
        vc.urlString = [NSString stringWithFormat:@"http://117.184.59.230:8090/DSFA/admin_client/questionnaire/moblie2.0/views/index.html?id=%@&accountid=%@&a=%@" , model.PSQ_ID , ZJUSERINFORMODEL.ORG_ACCOUNT.ID, model.WJID];
    }else{
        vc.urlString = [NSString stringWithFormat:@"http://117.184.59.230:8090/DSFA/admin_client/questionnaire/moblie2.0/views/index.html?id=%@&accountid=%@" , model.PSQ_ID , ZJUSERINFORMODEL.ORG_ACCOUNT.ID ];
    }
    vc.wjDCJF = model.PSQ_POINT;
    vc.wjtitle = model.PSQ_TITLE;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - getter
-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight + [UIScreen zj_screenWith] * 0.425, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight - [UIScreen zj_screenWith] * 0.425) style:UITableViewStylePlain];
        [_mainTableView registerClass:[YBQuestionnaireListTableViewCell class] forCellReuseIdentifier:QuestionnaireCell];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        [_mainTableView adaptiveIOS11];
    }
    return _mainTableView;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
